package main

import (
	"net/http"

	"gin_web/apps/book"
	"gin_web/apps/email"
	"gin_web/apps/test"
	"gin_web/orm"
	"gin_web/piugins/cloector"
	"github.com/gin-gonic/gin"
)

func main() {
	// 初始化DB， 迁移数据库
	orm.Migrate()

	router := gin.Default()

	router.StaticFS("/static", http.Dir("./static"))
	router.StaticFile("/favicon.ico", "/static/favicon.ico")
	router.LoadHTMLGlob("templates/**/*.html")

	router.NoRoute(func(c *gin.Context) {
		// 实现内部重定向
		c.HTML(http.StatusOK, "404.html", gin.H{
			"title": "404",
		})
	})

	router.GET("/", cloector.ExampleHandler)
	router.GET("/test", test.Test)

	boolUrlGroup := router.Group("/books")
	{
		boolUrlGroup.GET("/", book.BooksPage)
		boolUrlGroup.GET("/get_books", book.GetBooks)
		boolUrlGroup.GET("/query_books", book.GetBookByID)
		boolUrlGroup.POST("/books", book.CreateBook)
		boolUrlGroup.PUT("/books/:id", book.UpdateBook)
		boolUrlGroup.DELETE("/books/:id", book.DeleteBook)
	}

	tradeUrlGroup := router.Group("/trade")
	{
		tradeUrlGroup.POST("/create_book_trade", book.CreateBooksTrade)
	}

	emailUrlGroup := router.Group("/email")
	{
		emailUrlGroup.GET("/send_email", email.SendMessage)
	}

	_ = router.Run(":9999")

}
